<template>
  <div class="admin-login-page">
    <div class="login-container">
      <div class="login-header">
        <div class="logo">
          <h1>CarbonSpark 管理系统</h1>
        </div>
        <p class="welcome-text">管理员登录</p>
      </div>
      
      <div class="login-form">
        <div v-if="error" class="admin-alert admin-alert-error">
          {{ error }}
        </div>
        
        <div class="admin-alert admin-alert-info" style="margin-bottom: 20px;">
          提示：用户名和密码均为 <strong>admin</strong>
        </div>
        
        <div class="admin-form-item">
          <label class="admin-form-label" for="username">用户名</label>
          <input 
            id="username" 
            v-model="username" 
            type="text" 
            class="admin-form-input" 
            placeholder="请输入管理员用户名"
            @keyup.enter="handleLogin"
          >
        </div>
        
        <div class="admin-form-item">
          <label class="admin-form-label" for="password">密码</label>
          <input 
            id="password" 
            v-model="password" 
            type="password" 
            class="admin-form-input" 
            placeholder="请输入管理员密码"
            @keyup.enter="handleLogin"
          >
        </div>
        
        <div class="remember-me">
          <input type="checkbox" id="remember" v-model="remember">
          <label for="remember">记住我</label>
        </div>
        
        <button 
          class="login-button" 
          :disabled="loading" 
          @click="handleLogin"
        >
          <span v-if="loading">登录中...</span>
          <span v-else>登录</span>
        </button>
      </div>
      
      <div class="login-footer">
        <p>Copyright &copy; 2023 CarbonSpark</p>
        <p>低碳环保生活平台</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminLogin',
  data() {
    return {
      username: '',
      password: '',
      remember: false,
      loading: false,
      error: null
    }
  },
  methods: {
    async handleLogin() {
      if (!this.username || !this.password) {
        this.error = '请输入用户名和密码'
        return
      }
      
      this.loading = true
      this.error = null
      
      try {
        // 在实际应用中，这里应该调用登录API
        // const response = await adminLogin(this.username, this.password)
        
        // 模拟登录成功
        setTimeout(() => {
          // 简单验证，实际应用中应该与后端验证
          if (this.username === 'admin' && this.password === 'admin') {
            // 存储token，实际应用中应该使用后端返回的token
            localStorage.setItem('adminToken', 'admin-token-123')
            
            if (this.remember) {
              localStorage.setItem('adminUsername', this.username)
            } else {
              localStorage.removeItem('adminUsername')
            }
            
            // 登录成功，跳转到仪表盘
            this.$router.push('/admin/dashboard')
          } else {
            this.error = '用户名或密码错误 (提示：用户名/密码均为admin)'
          }
          
          this.loading = false
        }, 500)
      } catch (error) {
        this.error = error.message || '登录失败，请稍后重试'
        this.loading = false
      }
    }
  },
  created() {
    // 如果有保存的用户名，自动填充
    const savedUsername = localStorage.getItem('adminUsername')
    if (savedUsername) {
      this.username = savedUsername
      this.remember = true
    }
    
    // 如果已经登录，直接跳转到仪表盘
    if (localStorage.getItem('adminToken')) {
      this.$router.push('/admin/dashboard')
    }
  }
}
</script>

<style scoped>
.admin-login-page {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
  background-image: url('');
}

.login-container {
  width: 400px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 40px;
  animation: fadeInUp 0.5s;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo h1 {
  margin: 0;
  font-size: 24px;
  color: #1890ff;
}

.welcome-text {
  margin-top: 16px;
  font-size: 16px;
  color: #666;
}

.admin-form-item {
  margin-bottom: 24px;
}

.admin-form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.85);
}

.admin-form-input {
  width: 100%;
  height: 40px;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  transition: all 0.3s;
}

.admin-form-input:focus {
  border-color: #40a9ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
  outline: none;
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  user-select: none;
}

.remember-me input {
  margin-right: 8px;
}

.remember-me label {
  color: rgba(0, 0, 0, 0.65);
  cursor: pointer;
}

.login-button {
  width: 100%;
  height: 40px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.login-button:hover:not(:disabled) {
  background-color: #40a9ff;
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-footer {
  margin-top: 40px;
  text-align: center;
  color: #999;
  font-size: 14px;
}

.login-footer p {
  margin: 4px 0;
}

.admin-alert {
  padding: 8px 16px;
  border-radius: 4px;
  margin-bottom: 16px;
  font-size: 14px;
}

.admin-alert-error {
  background-color: #fff1f0;
  border: 1px solid #ffa39e;
  color: #ff4d4f;
}
</style> 